package com.yc.baihe.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yc.baihe.beans.BhUser;
import com.yc.baihe.beans.Story;
import com.yc.baihe.beans.UserList;
import com.yc.baihe.biz.IBhUserBiz;
import com.yc.baihe.biz.IUserListBiz;
import com.yc.baihe.biz.StoryBiz;
import com.yc.baihe.biz.impl.BhUserBizImpl;
import com.yc.baihe.biz.impl.StoryBizImpl;
import com.yc.baihe.biz.impl.UserListBizImpl;
import com.yc.baihe.utils.AgeUtil;
import com.yc.baihe.utils.SessionAttributeNames;


public class StoryInfoServlet extends CommonServlet {
	private static final long serialVersionUID = 1L;
	private HttpSession session;
	private PrintWriter out;
	private StoryBiz storyBiz=new StoryBizImpl();	//故事信息
	private IUserListBiz userListBiz=new UserListBizImpl(); //用户详细信息
	private IBhUserBiz bhUserBiz=new BhUserBizImpl(); //用户登录信息
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
			
			session=request.getSession();
			out=response.getWriter();
			
			if("storyData".equals(op)){
				storyData(request,response);
			}else if("storyDataofspace".equals(op)){
				storyDataofspace(request,response);
			}else if("storyZan".equals(op)){
				storyZan(request,response);
			}else if("addstory".equals(op)){
				addstory(request,response);
			}else if("addstory_story".equals(op)){
				addstory_story(request,response);
			}else if("getNow".equals(op)){
				getNow(request,response);
			}else if("manageStoryPic".equals(op)){
				manageStoryPic(request,response);
			}
			
		}
	
	/**
	 * 管理成功故事照片
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void manageStoryPic(HttpServletRequest request, HttpServletResponse response) throws IOException {
		BhUser bh=(BhUser) session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);
		int boy_id=bh.getBuid(); //当前用户的成功故事图片
		Story st=storyBiz.findByBoy_id(boy_id).get(0);	//当前用户的成功故事
		List<String> images=st.getTwo_photos();
		session.setAttribute(SessionAttributeNames.MINEPIC, images);
		response.sendRedirect("uploadstory4.jsp");
	}

	/**
	 * 编辑成功故事的重定向
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void getNow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String buid=request.getParameter("buid");	//boy_id
		if(storyBiz.isExistBid(Integer.parseInt(buid))>0){
			Story story=storyBiz.findByBoy_id(Integer.parseInt(buid)).get(0);
			session.setAttribute(SessionAttributeNames.UPLOADSTORY, story);
			String boyemail=bhUserBiz.findEmailFromBuid(story.getBoy_id());
			String girlemail=bhUserBiz.findEmailFromBuid(story.getGirl_id());
			session.setAttribute(SessionAttributeNames.BOYEMAIL, boyemail);
			session.setAttribute(SessionAttributeNames.GIRLEMAIL, girlemail);
		}else{
			session.setAttribute(SessionAttributeNames.UPLOADSTORY, null);
			session.setAttribute(SessionAttributeNames.BOYEMAIL, null);
			session.setAttribute(SessionAttributeNames.GIRLEMAIL, null);
		}
		
		response.sendRedirect("uploadstory.jsp");
	}

	/**
	 * 添加成功故事当前的故事内容
	 * @param request
	 * @param response
	 */
	private void addstory_story(HttpServletRequest request, HttpServletResponse response) {
		Object obj = session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);  
		BhUser uf = (BhUser)obj;
		int buid=uf.getBuid(); //当前用户序列号  也是创建该故事的boy_id
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String loveStatus=request.getParameter("loveStatus");  //恋爱了  结婚了  当副标题吧
		
		out.print(storyBiz.updataStoryByBoy_id(buid, title, loveStatus, content));
		out.flush();
		out.close();
	}

	/**
	 * 添加成功故事
	 * @param request
	 * @param response
	 */
	private void addstory(HttpServletRequest request, HttpServletResponse response) {
		String loveTab=request.getParameter("loveTab"); //爱情标签 用-分隔好的
		String timefrombh=request.getParameter("timefrombh");
		String boy_email=request.getParameter("boy_email").trim();
		String girl_email=request.getParameter("girl_email").trim();
		
		//先根据boy_email用户的登录邮箱  查询到用户的序列号
		int boy_id;
		int girl_id;
		try {
			String boy=bhUserBiz.findBuidFromEmail(boy_email);
			String girl=bhUserBiz.findBuidFromEmail(girl_email);
			boy_id = Integer.parseInt(boy);
			girl_id = Integer.parseInt(girl);
		} catch (Exception e) {
			out.println(2); //不存在男方登录账号或者女方登录账号
			e.printStackTrace();
			return;
		}
		
		Object obj = session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);  
		BhUser uf = (BhUser)obj;
		int buid=uf.getBuid(); //用户序列号
		
		if(boy_id!=buid){
			out.println(4);	//请输入自己的账号
			return;
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String marriage_time=sdf.format(new Date());
		Story st=new Story();
		st.setBoy_id(boy_id);
		st.setGirl_id(girl_id);
		st.setLoveTab(loveTab);
		st.setTimefrombh(timefrombh);
		st.setMarriage_time(marriage_time);
		
		if(storyBiz.isExistBid(boy_id)>0){ //如果成功故事表中已存在此buid
			out.println(storyBiz.updateStory(st));
		}else{
			out.println(storyBiz.addStory(st));
		}
		out.flush();
		out.close();
		
	}

	//点赞    能进来此方法的用户都没有点过赞     进入hzhspace.jsp 时必须先判断该用户有没有点过赞
	private void storyZan(HttpServletRequest request, HttpServletResponse response) {
		String totaltick=request.getParameter("totaltick").trim(); //点过赞的用户序列号
		int s_num =Integer.parseInt(request.getParameter("s_num").trim()); //此故事的编号
		Object obj = session.getAttribute(SessionAttributeNames.CURRENTUSERSINFO);   //获取当前登录用户
		BhUser uf = (BhUser)obj;
		int buid=uf.getBuid(); //用户序列号
		
		System.out.println("故事编号"+s_num);
		System.out.println("点过赞的用户"+totaltick);
		
		totaltick = totaltick+","+buid;  //形成新的点赞后的序列号    
		//根据故事的编号 更改数据库中此故事的totaltick值
		storyBiz.updataTotaltickBys_num(s_num, totaltick);
		
	}

	/**
	 * hzhspace 页面的重定向数据
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void storyDataofspace(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String s_num=request.getParameter("s_num");
		
		Story storydata=storyBiz.findByS_num(Integer.parseInt(s_num)).get(0);
		int boy_id=storydata.getBoy_id();
		int girl_id=storydata.getGirl_id();
		
		UserList space_boy=userListBiz.findbByBuids(boy_id).get(0);
		UserList space_girl=userListBiz.findbByBuids(girl_id).get(0);
		
		
		//把年龄存到 详细信息的备用字段temp1中
		int nianlingboy =AgeUtil.getAge(space_boy.getBirthday());
		int nianlinggirl =AgeUtil.getAge(space_girl.getBirthday());
		space_boy.setTemp1(String.valueOf(nianlingboy));
		space_girl.setTemp1(String.valueOf(nianlinggirl));
		
		session.setAttribute(SessionAttributeNames.SPACEBOY, space_boy);
		session.setAttribute(SessionAttributeNames.SPACEGIRL,space_girl);
		
		session.setAttribute(SessionAttributeNames.SPACEDATA, storydata);
		
		String currentbuid=(String) session.getAttribute("currentBuid"); //当前用户序列号
		boolean iszanUser=false;
		
		String zan=storydata.getTotaltick();
		String[] zanUsers=zan.split(",");
		for(String a:zanUsers){
			if(a.equals(currentbuid)){
				iszanUser=true;
			}else{
				continue;
			}
		}
		session.setAttribute(SessionAttributeNames.ISZANUSER, iszanUser);
		
		response.sendRedirect("hzhspace.jsp");
	}


	/**
	 * 秀恩爱故事页面数据准备函数
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void storyData(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("进来了");
		//List<Story> list=storyBiz.find(null, 4);
		//session.setAttribute(SessionAttributeNames.STORYDATA, list);
		//response.sendRedirect("info.jsp");
	}

}
